#include "com.hpp"

void test1()
{
    //1、创建key值
    key_t key = Getkey();
    cout<<"Getkey():"<<key<<endl;

    //2、创建共享内存
    int shmid = GetShm(key,shmsize);
    cout<<"CreatShm(key,shmsize),shmid:"<<shmid<<endl;

    //3、server进程拿到共享内存的起始地址，进行关联
    char* start = AttachShm(shmid);
    char c = 'A';
    while(c <= 'Z')
    {
        start[c-'A'] = c;
        c++;
        start[c-'A'] = 0;
        sleep(1);
    }

     snprintf(start, shmsize, "EXIT");


    //4、server进程和共享内存解除关联
    DetachShm(start);

}

void test2()
{
    Shm t(0);
    char* start = t.GetStart();
    char c = 'A';
    while(c <= 'D')
    {
        start[c-'A'] = c;
        c++;
        start[c-'A'] = 0;
        sleep(1);
    }

    snprintf(start, shmsize, "EXIT");
    cout<<"client:发送结束"<<endl;

}



//共享内存分为三步
//1、先建立，然后让其链接
//2、客户发，服务器接受
//3、客户先关闭，服务器再关
int main()
{

    //test1();//无封装

    test2();//有封装
    
    return 0;
}